package com.db.sql;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.excel.read.DBTable;

/**
 * 查询树
 * 
 * @author mike
 * 
 */
public class SqlNode extends Node {
	/**
	 * 父节点，一张表可以有很多外键，所以应该有很多的父节点
	 */
	private Map<String, SqlNode> next = new HashMap<String, SqlNode>();
	private String nodeName;
	/**
	 * 子节点，一张表可以对应很多的子节点，但目前只实现单子节点
	 * TODO
	 */
	private SqlNode prev;
	private String fullPath;
	private String displayName;
    private String qName;
	private DBTable table;

	private Map<String, SqlLeafNode> leaves = new HashMap<String, SqlLeafNode>();

	public static final int PARENAT = 1;
	public static final int MIDDLE = 2;
	public static final int CHILD = 4;
	/**
	 * 节点的级别 用于事件驱动
	 */
	public int LEVEL = 4;

	/**
	 * 没一个节点都有一个唯一标示的名字, 用于英文区分
	 * 
	 * @return
	 */

	private String uniqName = "";

	
	
	public SqlNode() {
		
		super();
		this.qName = NodeNameSequence.getNext();
	}

	public Map<String, SqlNode> getNext() {
		return next;
	}

	public void setNext(Map<String, SqlNode> next) {
		this.next = next;
	}

	public String getNodeName() {
		return nodeName;
	}

	public void setNodeName(String nodeName) {
		this.nodeName = nodeName;
	}

	public SqlNode getPrev() {
		return prev;
	}

	/**
	 * 设置子
	 * @param prev
	 */
	//TODO 目前只有一个子
	public void setPrev(SqlNode prev) {
		this.prev = prev;
	}

	public String getUniqName() {
		return uniqName;
	}

	public void setUniqName(String uniqName) {
		this.uniqName = uniqName;
	}

	public void putLeaf(String nodeName, SqlLeafNode leaf) {
		this.leaves.put(nodeName, leaf);
	}

	public SqlLeafNode getLeaf(String nodeName) {
		return this.leaves.get(nodeName);
	}

	public Map<String, SqlLeafNode> getLeaves() {
		return leaves;
	}

	public void setLeaves(Map<String, SqlLeafNode> leaves) {
		this.leaves = leaves;
	}

	/**
	 * 父子关系
	 * 
	 * @param nodeName
	 * @param leaf
	 */
	public void connect(String parentNodeName, SqlNode parentNode) {
		if (this.next.containsKey(parentNodeName)) {

		} else {
			putNext(parentNodeName, parentNode);
			parentNode.setPrev(this);
		}
	}

	public void putNext(String nodeName, SqlNode leaf) {
		this.next.put(nodeName, leaf);
	}

	public SqlNode getNext(String nodeName) {
		return this.next.get(nodeName);
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public DBTable getTable() {
		return table;
	}

	public void setTable(DBTable table) {
		this.table = table;
	}
	/**
	 * 是否是最顶层
	 * @return
	 */
	public boolean isTopParent() {
		boolean flag = true;
		if (this.next.size() == 0) {
			// LEVEL = SqlNode.PARENAT;
		} else {
			flag = false;
		}
		return flag;
	}

	public String getQName() {
		return qName;
	}

	public void setQName(String name) {
		qName = name;
	}
	
	

}
